﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rodunu.Business.Interfaces;
using Rodunu.Clients.WPF.Services.Netflix;

namespace Rodunu.Clients.WPF.Settings
{
    /// <summary>
    /// Interaction logic for Accounts.xaml
    /// </summary>
    public partial class Accounts : UserControl
    {
        private Main _mainForm;
        private readonly IManageSettings _manageSettings;

        public Accounts(IManageSettings manageSettings)
        {
            InitializeComponent();

            _manageSettings = manageSettings;

            var netflixButton = new AccountButton();
            netflixButton.MouseDown += netflixButton_MouseDown;
            AccountWrapPanel.Children.Add(netflixButton);

            var amazonButton = new Services.Amazon.AmazonButton();
            amazonButton.MouseDown += amazonButton_MouseDown;
            AccountWrapPanel.Children.Add(amazonButton);

            var imdbButton = new Services.IMDB.IMDBButton();
            imdbButton.MouseDown += imdbButton_MouseDown;
            AccountWrapPanel.Children.Add(imdbButton);

            AccountWrapPanel.Children.Add(new Services.Apple.AppleButton());
            AccountWrapPanel.Children.Add(new Services.Fandango.FandangoButton());
            AccountWrapPanel.Children.Add(new Services.Google.GoogleButton());
            AccountWrapPanel.Children.Add(new Services.Hulu.HuluButton());
            AccountWrapPanel.Children.Add(new Services.ITunes.ITunesButton());
            AccountWrapPanel.Children.Add(new Services.PirateBay.PirateBayButton());
            AccountWrapPanel.Children.Add(new Services.RottenTomatoes.RottenTomatoesButton());
            AccountWrapPanel.Children.Add(new Services.Wikipedia.WikipediaButton());
            AccountWrapPanel.Children.Add(new Services.Yahoo.YahooButton());
            AccountWrapPanel.Children.Add(new Services.Youtube.YoutubeButton());
        }

        void imdbButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var detailPage = new Services.IMDB.DetailPage(_manageSettings);
            detailPage.CloseButton.Click += CloseButton_Click;
            SetMainForm(this, detailPage);
        }

        void amazonButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var detailPage = new Services.Amazon.DetailPage(_manageSettings);
            detailPage.CloseButton.Click += CloseButton_Click;
            SetMainForm(this, detailPage);
        }

        void netflixButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var netflixPage = new DetailPage(_manageSettings);
            netflixPage.CloseButton.Click += CloseButton_Click;
            SetMainForm(this, netflixPage);
        }

        void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            _mainForm.MaiWindow.Children.RemoveAt(_mainForm.MaiWindow.Children.Count - 1);
        }

        private void SetMainForm(FrameworkElement control, UIElement controlToAdd)
        {
            if (_mainForm != null)
                _mainForm.MaiWindow.Children.Add(controlToAdd);
            else
            {
                FindMainForm(control);
            }
        }

        private void FindMainForm(FrameworkElement control)
        {
            if (control.Parent.GetType().BaseType.Name == "Window")
            {
                _mainForm = (Main)control.Parent;
            }
            else
            {
                FindMainForm((FrameworkElement)control.Parent);
            }
        }
    }
}
